#ifndef CHAT_CINEMA_H
#define CHAT_CINEMA_H

#include <map>
#include "TcpConnection.hpp"
#include <vector>
#include <set>
#include <chrono>


class ChatCinema {
public:
    ChatCinema(const std::string& roomName, const std::string& hostname, ConnectionPtr hostConnection=nullptr); 
    ~ChatCinema();
    bool addUser(const std::string& username, ConnectionPtr userConnection=nullptr);

    void removeUser(const std::string& username);
    bool haveUser(const std::string& username);

    inline const std::vector<std::pair<std::string, std::string>>&  getHistory(){
        return chatHistory_;
    }

    inline const std::map<std::string, std::weak_ptr<TcpConnection>>& getConnections() {
        return userConnections_;
    }
    
    inline void addHistory(const std::string& username, const std::string& message) {
        chatHistory_.emplace_back(username, message);
    }

    inline const std::string& getRoomName(){
        return roomName_;
    }

    inline void setRoomName(const std::string& roomName) {
        roomName_ = roomName;
    }

    inline void setFilmName(const std::string& filmname) {
        filmName_ = filmname;
    }

    inline int getUserNum(){
        return users_.size();
    }

    using ShutDownCallBackPtr = std::shared_ptr<std::function<void(const std::string&)>>;
    inline void setShutDownCall(ShutDownCallBackPtr callback) {
        onShutDown = callback;
    }

    



private:
    std::map<std::string, std::weak_ptr<TcpConnection>> userConnections_; // 不能用sharedptr，否则Connection类在close之后将无法销毁。
    std::string roomHost_;
    std::vector<std::pair<std::string, std::string>> chatHistory_;
    std::set<std::string> users_;

    std::string roomName_;
    std::string filmName_;

    int timestamp_=0; // 放映的时间戳

    

    ShutDownCallBackPtr onShutDown;




};


#endif